package com.sk.bilibili.view.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.sk.bilibili.R;
import com.sk.bilibili.callBack.OnBottomCallBack;
import com.sk.bilibili.callBack.OnScrollBottomListener;
import com.sk.bilibili.databinding.FragmentRefreshBinding;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.model.BaseRefreshModel;

public abstract class BaseRefreshFragment<M extends BaseRefreshModel> extends
        BaseFragment<FragmentRefreshBinding,M> implements SwipeRefreshLayout.OnRefreshListener, OnBottomCallBack {

    @Override//  可以刷新的页面 应该先 刷新 阻止 model.loadData();
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        afterSetView();
        bind.srlShow.setRefreshing(true);//默认开启刷新
        onRefresh();//把  加载更多事件改为刷新事件   使用  onRefresh  方便子类进行自定义刷新事件
    }

    @Override
    protected void afterSetView() {
        initView();
        bindEvent();
    }

    private void bindEvent() {
        bind.srlShow.setOnRefreshListener(this);
        bind.rvShow.addOnScrollListener(new OnScrollBottomListener(this));
    }

    protected DelegateAdapter adapter;

    private void initView() {
        bind.srlShow.setColorSchemeResources(R.color.pink);
        VirtualLayoutManager manager = new VirtualLayoutManager(getContext());
        bind.rvShow.setLayoutManager(manager);
        adapter = new DelegateAdapter(manager);
        initAdapter(adapter);
        bind.rvShow.setAdapter(adapter);
    }

    protected abstract void initAdapter(DelegateAdapter adapter);

    protected void cancelRefresh(){
        bind.srlShow.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        model.refreshData();
    }

    @Override
    public void onBottom() {
        model.loadData();
    }

}